﻿using System;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Globalization;
using Medianamik.UI.Controls;

namespace Medianamik.UI.ConfigManagement
{
    public partial class MenuGroupManagement : BaseModuleEditionPage<MenuGroup>
    {
        protected const string LIST_PAGE = "PageList.aspx?tab=2";

        protected  void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)// && !String.IsNullOrEmpty(CurrentNode.Name))
            {
                var menuGroupDescriptions = (TranslatorBox)MedianamikFormTemplate.FindControl("MenuGroupDescriptions");
                menuGroupDescriptions.Values = Translator.GetResources("SiteMap",
                    "menuGroup_Description_" + CurrentNode.ID.ToString("N"),
                    ConfigManager.GetMedianamikCultures().Select(c => c.CultureInfo));
            }
        }

        protected override void Save(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var menu = MedianamikFormTemplate.GetFormValue(CurrentNode);

                NodeManager.SaveNode(menu);

                var menuGroupDescriptions = (TranslatorBox)MedianamikFormTemplate.FindControl("MenuGroupDescriptions");
                Translator.CreateResource("SiteMap", "menuGroup_Title_" + menu.ID.ToString("N"), menu.GetValues<String>("Name"));
                Translator.CreateResource("SiteMap", "menuGroup_Description_" + menu.ID.ToString("N"), menuGroupDescriptions.Values);

                //Reset du Menu
                ((MedianamikSiteMapProvider)SiteMap.Provider).Rebuild();

                Response.Redirect(ReturnURL);
            }
        }

        protected override string ReturnURL
        {
            get { return LIST_PAGE; }
        }

        public override MedianamikFormTemplate MedianamikFormTemplate
        {
            get { return Fields; }
        }

        public override Guid? TypeId
        {
            get
            {
                return MedianamikTypes.MedianamikMenuGroup;
            }
        }

        public override LinkButton SaveButton
        {
            get { return btnSave; }
        }

        protected override Guid? ParentNodeId
        {
            get
            {
                return MedianamikModules.ConfigManagement;
            }
        }
    }
}
